function ManageModel(options){
    var self = this;
    var name = options.itemName;
    var url = options.actions ? options.actions.url : null;
    
    // ------------------------------------------------------- settings
    this.defaults = {
        actions: {
            a: true,
            e: true,
            d: true
        },
        tableOptions: {
            table: $('#dataTable'),
            title: options.title,
            target: $('div.target'),
            refreshUrl: 'all',
            options: {
                bAutoWidth: false
            },
            updateToolbar: function(){
                var hasSelected = self.dt.getSelected().size()>0;
                if(self.settings.actions.e) self.editButton[0].wrapper.setEnabled(hasSelected);
                if(self.settings.actions.d) self.deleteButton[0].wrapper.setEnabled(hasSelected);
            }
        }
    }
    
    var settings = this.settings = $.extend(true, {}, this.defaults, options);
    
    // ------------------------------------------------------- methods
    this.addEdit = function(isEdit){
        var id; 
        if(isEdit){
            id = self.getId();
            if(id==null) return;
        }
        
        dialogManager.open({
            url: isEdit ? url+'e' : url+'a',
            data: {
                id: id
            },
            width: 500,
            resizable: false,
            title: 'Add Step'
        })
    }
    
    this.submit = function(data, isEdit){
        $.myAjax({
            url: 's',
            type: "POST",
            data: data,
            async: false,
            success: function(result) {
                Notifier.success(name+' was '+(isEdit ? 'updated' : 'added')+'!');
                dialogManager.closeDialog();
                self.dt.refresh();
            }
        });
    }
    
    this.remove = function(){
        var id = self.getId();
        if(id==null) return;
        confirmDialog({
            text: 'delete '+name.toLowerCase(),
            callback: function(){
                $.myAjax({
                    url: dialogManager.getBaseUrl() + url + 'd',
                    data: {
                        id: id
                    },
                    type: "POST",
                    success: function(result) {
                        Notifier.success(name+' was deleted!');
                        self.dt.refresh();
                    }
                });
            }
        })
    }
    
    this.getId = function(){
        return this.dt ? this.dt.getSelectedId() : null;
    }
    
    // ------------------------------------------------------- creation
    var buttons = new Array();
    if(settings.actions && settings.actions.url){
        // prepares add button
        if(settings.actions.a){
            this.addButton = uic.element.button({
                hint: 'Add '+name,
                primary:  true,
                icon: 'icon-plus icon-white',
                onclick: function(){
                    self.addEdit();
                }
            }).getElement();
            
            buttons.push(this.addButton);
        }
        
        if(settings.actions.e){
            this.editButton = uic.element.button({
                hint: 'Edit '+name,
                icon: 'icon-edit',
                onclick: function(){
                    self.addEdit(true);
                }
            }).getElement();
            
            buttons.push(this.editButton);
        }

        if(settings.actions.d){
            this.deleteButton = uic.element.button({
                hint: 'Delete '+name,
                icon: 'icon-trash',
                onclick: self.remove
            }).getElement();
            
            buttons.push(this.deleteButton);
        }
    }
    if(settings.tableOptions.buttons){
        for(var i=0;i<settings.tableOptions.buttons.length; i++){
            buttons.push(settings.tableOptions.buttons[i]);
        }
    }
    if(settings.tableOptions.buttonGroups){
        settings.tableOptions.buttonGroups.unshift(buttons);
    } else {
        settings.tableOptions.buttonGroups = new Array(buttons);
    }
    
    // prepares DataTable
    this.dt = uic.table(settings.tableOptions);
}
